package com.itbour.web.action.tg;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.util.StringUtils;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.tg.SearchTagInfoDto;
import com.itbour.service.tg.SearchTagInfoService;
import com.opensymphony.xwork2.ActionContext;

public class GetSearchTagInfoAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private SearchTagInfoService searchTagInfoService;
	private String findTitleName;//标题名称
	private String searchTagType;//根据分类来查询
	HashMap<String, Object> SearchTagInfoResult = new HashMap<String, Object>();
	private SearchTagInfoDto searchInfo;
	private List<SearchTagInfoDto> searchInfoList;
	public String getSearchTagInfoPg() throws Exception{
		// 用户信息
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null) {
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageNumber = request.getParameter("page");
			String pageSize = request.getParameter("rows");
			Map<String, Object> param = new HashMap<String, Object>();
			if(StringUtils.isNotBlank(findTitleName)){
				param.put("type_name", findTitleName);
			}

			if(StringUtils.isNotBlank(searchTagType)){
				param.put("type", searchTagType);
			}
			// 默认只检索自己的文档([超级管理员,模版操作员]除外)
			if(!user.isSuperUser() && (user.getRole()&32)!=32) {
				param.put("user_id", 3);
			}
			
			//翻页查询
			Page page = searchTagInfoService.pageTagInfoPg(param, Integer.parseInt(pageNumber),
					Integer.parseInt(pageSize));
			HashMap<String, Object> resMap = new HashMap<String, Object>();
			resMap.put("total", page.getTotalCount());
			resMap.put("rows", page.getResult());
			SearchTagInfoResult = resMap;
		} else {
			SearchTagInfoResult.clear();
		}
		return SUCCESS;
	}

	// 修改专题显示顺序
	public void updSearchOrder() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String id=request.getParameter("orderid");
		String order=request.getParameter("order");
		if (StringUtils.isNotBlank(id)) {
			Map<String, Object> session = ActionContext.getContext().getSession();
	    	UserInfoDto user = (UserInfoDto)session.get("user");
	    	// update
	    	if(user!=null && user.getUser_id()!=null){
		    	Map<String, Object> param = new HashMap<String, Object>();
				param.put("number", Integer.valueOf(order));
				param.put("id", Long.valueOf(id));
				param.put("update_by", user.getUser_id());
				searchTagInfoService.updSearch(param);
	    	}
		}
		this.writeJSON("ok");
	}
	// 删除专题信息
	public void delSearch() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String ids = request.getParameter("ids");
		searchTagInfoService.delSearch(ids);
		this.writeJSON("ok");
	}
	public void addSearch() throws Exception{

		if (searchInfo != null) {
			Map<String, Object> session = ActionContext.getContext().getSession();
	    	UserInfoDto user = (UserInfoDto)session.get("user");
	    	if (user != null) {
	    		searchInfo.setUpdate_by(user.getUser_id().toString());
	    	}
	    	
	    	if(searchInfo.getTag_id()!=null &&searchInfo.getTag_id()>0){
	    		// 更新
	    		this.updSearch();
	    	} else {
	    		// 新建
	    		searchTagInfoService.addSearch(searchInfo);
	    		this.writeJSON("ok");
	    	}
		} else {
			this.writeJSON("ok");
		}
	
	}
	

	private void updSearch() throws Exception{
		if (searchInfo != null && searchInfo.getTag_id()>0) {
			Map<String, Object> session = ActionContext.getContext().getSession();
	    	UserInfoDto user = (UserInfoDto)session.get("user");
	    	
	    	Map<String, Object> param = new HashMap<String, Object>();
	    	param.put("id", searchInfo.getTag_id());
	    	if(user != null) {
	    		param.put("update_by", user.getUser_id());
	    	}
	    	if(searchInfo.getType()>0) {
	    		param.put("type", searchInfo.getType());
	    	}

	    	if(searchInfo.getName()!= null) {
	    		param.put("name", searchInfo.getName());//标题
	    	}
	
	       	if(searchInfo.getCreate_time()!= null) {
	    		param.put("start_time", searchInfo.getCreate_time());
	    	}
	       	searchTagInfoService.updSearch(param);
		}
		this.writeJSON("ok");
	

		
	}

	public String getSearchInfoEdit() throws Exception{
		HttpServletRequest request = ServletActionContext.getRequest();
		Map<String, Object> param = new HashMap<String, Object>();
		String id = request.getParameter("id");
		if (StringUtils.isNotEmpty(id)) {
			param.put("id", id);
		}
		searchInfoList = searchTagInfoService.selectSearchInfo(param);
		return SUCCESS;
	}
	
	
	public SearchTagInfoService getSearchTagInfoService() {
		return searchTagInfoService;
	}

	public String getFindTitleName() {
		return findTitleName;
	}

	public String getSearchTagType() {
		return searchTagType;
	}

	public HashMap<String, Object> getSearchTagInfoResult() {
		return SearchTagInfoResult;
	}

	public void setSearchTagInfoService(SearchTagInfoService searchTagInfoService) {
		this.searchTagInfoService = searchTagInfoService;
	}

	public void setFindTitleName(String findTitleName) {
		this.findTitleName = findTitleName;
	}

	public void setSearchTagType(String searchTagType) {
		this.searchTagType = searchTagType;
	}

	public void setSearchTagInfoResult(HashMap<String, Object> searchTagInfoResult) {
		SearchTagInfoResult = searchTagInfoResult;
	}

	public SearchTagInfoDto getSearchInfo() {
		return searchInfo;
	}

	public void setSearchInfo(SearchTagInfoDto searchInfo) {
		this.searchInfo = searchInfo;
	}

	public List<SearchTagInfoDto> getSearchInfoList() {
		return searchInfoList;
	}

	public void setSearchInfoList(List<SearchTagInfoDto> searchInfoList) {
		this.searchInfoList = searchInfoList;
	}
}
